<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\controller\Fornt;
use app\common\model\Coupon as CouponModel;
use app\common\model\Cart as CartModel;
use app\common\model\CouponList;
use app\common\model\ActionLog;
use think\Request;

class Coupon extends Fornt{

	public function index(){
    //未使用的优惠券
    $list = CouponList::where(['uid'=>USER_ID,'status'=>0])->whereTime('deadline', '>=', date('Y-m-d H:m:s',	time()))->order('id desc')->select();
    $this->assign('list', $list);

    //商品总价格
    $cinfo = CartModel::getNumAndPrice(['user_id'=>USER_ID]);
		$this->assign('cartCountprice', $cinfo['price']);


    return $this->fetch();
	}

	public function getcouponlist(){
    //未使用的优惠券
    $list = CouponList::where(['uid'=>USER_ID,'status'=>0])->order('id desc')->select();
    return json($list);
	}
	public function setdefaults($id = null){
		if (!$id) {
			return $this->error("非法操作！");
		}
		AddressModel::where('user_id', USER_ID)->update(['defaults' => 0]);
		$info = AddressModel::find($id);
		$info->defaults = 1;
		$result=	$info->save();
		if ($result) {
				return json($info);
		} else {
				return $this->error("设置失败！");
		}
	}
	//积分兑换中心
	public function integral(){

		return $this->fetch();
	}
	
	//积分兑换中心
	public function integraltest(){
	
	    return $this->fetch();
	}
	//积分兑换
	public function exchange(){
		if (IS_POST) {
				$data = input('post.');
				//判断现有积分大于兑换的积分
				$userInfo = session('user');
				if($userInfo['integral']<$data['integral']){
					$this->error('亲,你的积分还未达到'.$data['integral'].'分');
				}
				$date=date('Y-m-d H:m:s',	strtotime('2038-01-01'));
				//生成优惠券
				$couponData = ['coupon_id'=>2,'uid'=>USER_ID,'code'=>date('md').rand_number(4),'total'=>$data['total'],'price'=>$data['coupon'],'deadline'=>$date];

				$return = model('CouponList')->save($couponData);
				if($return){
					$user = model('WxUser')->find(USER_ID);
					$user->integral = $user['integral']-$data['integral'];
					$userInfo = $user->save();
					session('user', $userInfo);
					exchange_log('exchange_integral', 'CouponList', USER_ID, USER_ID, $data);
				}
				$this->success('兑换成功！');
    }
	}
	
	//红包卷兑换
	public function exchangetest(){
	    if (IS_POST) {
	        $data = input('post.');
	        //判断现有积分大于兑换的积分
	        $userInfo = session('user');
	        if($userInfo['integral']<$data['integral']){
	            $this->error('亲,你的积分还未达到'.$data['integral'].'分');
	        }
	        
	        $date=date('Y-m-d H:m:s',	strtotime('2038-01-01'));
	        //生成优惠券
	        $couponData = ['coupon_id'=>2,'uid'=>USER_ID,'code'=>date('md').rand_number(4),'total'=>$data['total'],'price'=>$data['coupon'],'deadline'=>$date];
	
	        $return = model('CouponList')->save($couponData);
	        if($return){
	            $user = model('WxUser')->find(USER_ID);
	            $user->integral = $user['integral']-$data['integral'];
	            $user->useredsum = $user['useredsum']+1;
	            $userInfo = $user->save();
	            session('user', $userInfo);
	            exchange_log('exchange_integral', 'CouponList', USER_ID, USER_ID, $data);
	        }
	        $this->success('兑换成功！');
	    }
	}
	
	
	//积分历史
	public function integralHistory(){
		$list = ActionLog::where(['user_id'=>USER_ID,'action_id'=>array('in', '0,1,3')])->order('id desc')->paginate(10);
    $this->assign('list', $list);
		return $this->fetch();
	}

	public function exchangeHistory(){
		$list = ActionLog::where(['user_id'=>USER_ID,'action_id'=>2])->order('id desc')->paginate(10);
    $this->assign('list', $list);
		return $this->fetch();
	}
//红包
	public function exchangeHistorytest(){
	    $list = ActionLog::where(['user_id'=>USER_ID,'action_id'=>2])->order('id desc')->paginate(10);
	    $this->assign('list', $list);
	    return $this->fetch();
	}
	public function checkVoucherCode($code=null){
		$info = db('coupon_list')->where('code', $code)->find();
		if(!$info){
			$this->error('优惠券代码无效');
		}
		if($info['uid']){
			$this->error('优惠券已经被绑定了');
		}else{
			$return = db('coupon_list')->where('code', $code)->update(['uid' => USER_ID]);
			$this->success('优惠券绑定成功！');
		}
	}


}
